// =====================================================================================
//
//       Filename:  mkstemp.c
//
//    Description:  mkstemp函数的使用
//
//        Version:  1.0
//        Created:  12/04/2014 08:42:02 PM
//       Revision:  none
//       Compiler:  g++
//
//         Author:   (), 
//        Company:  
//
// =====================================================================================

#include "../apue.h"
#include "../myerr.h"
#include <errno.h>
int log_to_stderr = 1;

void make_temp(char* template);

int main()
{
    char    good_template[] = "/tmp/dirXXXXXX";     //right way
    char    *bad_template = "/tmp/dirXXXXXX";       //wrong way

    printf("trying to create first temp file...\n");
    make_temp(good_template);
    printf("trying to create second temp file...\n");
    make_temp(bad_template);

    return 0;
}

void make_temp(char* template)
{
    int fd;
    struct stat sbuf;
    if ( (fd = mkstemp(template)) < 0 )
        err_sys("can't create temp file");
    printf("temp name = %s\n", template);
    close(fd);
    if ( stat(template, &sbuf) < 0 )
    {
        if ( errno == ENOENT )
            printf("file doesn't exist\n");
        else
            err_sys("stat failed\n");
    }
    else
    {
        printf("file exists\n");
        unlink(template);
    }
}
